package DesignPattern.Builder;

/**
 * @Author self
 * @Date 2024/8/11 17:14
 */
public class Computer {
    // 必需参数
    private String CPU;
    private String RAM;

    // 可选参数
    private int storage;
    private boolean hasGraphicsCard;
    private boolean hasBluetooth;

    // 私有构造函数，Builder 负责创建实例
    private Computer(Builder builder) {
        this.CPU = builder.CPU;
        this.RAM = builder.RAM;
        this.storage = builder.storage;
        this.hasGraphicsCard = builder.hasGraphicsCard;
        this.hasBluetooth = builder.hasBluetooth;
    }

    @Override
    public String toString() {
        return "Computer [CPU=" + CPU + ", RAM=" + RAM + ", Storage=" + storage +
                "GB, GraphicsCard=" + hasGraphicsCard + ", Bluetooth=" + hasBluetooth + "]";
    }

    // 静态 Builder 类
    public static class Builder {
        // 必需参数
        private String CPU;
        private String RAM;

        // 可选参数 - 初始化为默认值
        private int storage = 256;
        private boolean hasGraphicsCard = false;
        private boolean hasBluetooth = false;

        // Builder 构造函数，带必需参数
        public Builder(String CPU, String RAM) {
            this.CPU = CPU;
            this.RAM = RAM;
        }

        // 设置存储
        public Builder setStorage(int storage) {
            this.storage = storage;
            return this;
        }

        // 设置显卡
        public Builder setGraphicsCard(boolean hasGraphicsCard) {
            this.hasGraphicsCard = hasGraphicsCard;
            return this;
        }

        // 设置蓝牙
        public Builder setBluetooth(boolean hasBluetooth) {
            this.hasBluetooth = hasBluetooth;
            return this;
        }

        // 构建方法
        public Computer build() {
            return new Computer(this);
        }
    }
}

